﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;

namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/submitRobosimScore")]
	public class SubmitRobosimScoreController : Controller
	{
		[HttpPost]
		public IActionResult SubmitRobosimScore([FromForm] SubmitRobosimScoreRequest model)
		{
			Account account = new Account(model.user_id);
			Round round = new Round(model.round_id);
			Group group = new Group(round.GroupId);
			Event @event = new Event(group.event_id);
			int[] join = account.GetAllJoinedEvents();
			if (Array.IndexOf(join, group.id) == -1)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "Has not apply" });
			}
			Score score = new Score(@event.db_name, account.user_id, round.Id);
			if (score.user_id <= 0)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "Has not apply" });
			}
			long now = Tools.GetTimestamp();
			if (now > round.EndTime)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "The game is over" });
			}
			if(now < round.StartTime)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "_GAME_NOT_START_" });
			}
			if(round.IsExam==1 && score.limit != 1)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "_UNABLE_TO_RESUBMIT_SCORE" });
			}
			if (round.IsExam==1 && score.sub_limit <=0)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "_OVER_SUBMIT_LIMIT_" });
			}
			if (score.final_score >= model.final_score)
			{
				return Ok(new ErrorResponse() { status = -1, errmsg = "_SCORE_LOWWER_THAN_LAST_" });
			}
			if (round.IsExam==1)
			{
				score.limit = 0;
			}
			if (round.IsLimit == 1)
			{
				score.sub_limit -= 1;
			}
			score.score = model.score;
			score.final_score = model.final_score;
			score.duration = model.duration;
			score.use_robot = model.use_robot;
			score.local_time = model.local_time;
			score.Update();
			SubmitRobosimScoreResponse response = new SubmitRobosimScoreResponse() { status = 1, data = new object[] { score.score, score.final_score, score.duration, score.local_time } };
			response.status = 1;
			response.data = new object[] { score.final_score, score.duration, score.user_id,score.local_time };
			return Ok(response);
		}
		//user_id=1&group_id=1&map_id=1&score=10&final_score=10&local_time=2024-09-17%2009%3a38%3a05&duration=1.31&use_robot=Car3&duration_valid=0&version=2.3.1.135&round_id=1
		public class SubmitRobosimScoreRequest
		{
			public int user_id { get; set; }
			public int group_id { get; set; }
			public int map_id { get; set; }	
			public decimal score { get; set; }
			public decimal final_score { get; set; }
			public string local_time { get; set; }
			public decimal duration { get; set; }
			public string use_robot { get; set; }
			public int duration_valid { get; set; }
			public string version { get; set; }
			public int round_id { get; set; }
		}
		//{"status":1,"data":[10,4.55,1,"2024-09-16 22:37:31"]}
		public class SubmitRobosimScoreResponse
		{
			public int status { get; set; }
			public object[] data { get; set; }
		}
	}
}
